package net.hyperz.hackit;

import android.app.Activity;
import android.graphics.drawable.AnimationDrawable;

import android.os.Bundle;
import android.os.Handler;
import android.os.SystemClock;

import android.widget.ImageView;
import android.widget.TextView;

public class Battery extends Activity {

	long mStartTime = 0L;
	int charge = 0;
	int currentLevel = 0;
	
	AnimationDrawable chargingAnimation;
	ImageView mBatteryImageView = null;
	TextView mChargeValueTextView = null;

	static final int totalPower = 2500;
	static final int updateInMillis = 60000;

	private Handler mHandler = new Handler();

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.battery);
		
		//grab the views we will be handling
		mChargeValueTextView = (TextView) findViewById(R.id.battery_value);
		mBatteryImageView = (ImageView) findViewById(R.id.battery_image);
	}

	@Override
	public void onWindowFocusChanged(boolean hasFocus) {
		super.onWindowFocusChanged(hasFocus);

		//start charging animation level 1
		switchBatteryImage(0, true, false);

		mHandler.removeCallbacks(mUpdateTimeTask);
		mHandler.post(mUpdateTimeTask);
	}

	private Runnable mUpdateTimeTask = new Runnable() {
		public void run() {
			//record the millis seconds of when we started
			mStartTime = SystemClock.uptimeMillis();

			//set the current value text
			mChargeValueTextView.setText(Integer.toString(charge) + "/2500 (" + Long.toString(Math.round(((double)charge / totalPower) * 100)) + "%)");
			charge++;	//increase the charge

			if (charge > totalPower) {
				//battery is full!
				switchBatteryImage(5, false, true);
			} else {
				//check our battery level
				int level = Math.round(((float)charge / totalPower) * 5);
				if (level != currentLevel) {
					//battery level has changed, update animation to reflect this
					switchBatteryImage(level, true, false);
					currentLevel = level;	//set new level
				}

				//check if any time elapsed since we started
				long millis = SystemClock.uptimeMillis() - mStartTime;
				//Log.i("Main", "msecs that have elapsed since start " + millis);

				//setup a new call to this runnable to execute in the set msecs minus any elapsed msecs
				mHandler.postAtTime(this, SystemClock.uptimeMillis() + (updateInMillis - millis));
				//Log.i("Main", "Next run in " + (updateInMillis - millis) + " msecs");
			}
		}
	};

	private void startBatteryChargeAnim(int resourceId) {
		mBatteryImageView.setImageResource(resourceId);
		((AnimationDrawable) mBatteryImageView.getDrawable()).start();
	}

	private void startNewBatteryChargingAnim(int batteryLevel) {
		int resId = R.drawable.charging_battery_anim;

		switch (batteryLevel) {
		case 1:
			resId = R.drawable.charging_battery_anim_1;
			break;
		case 2:
			resId = R.drawable.charging_battery_anim_2;
			break;
		case 3:
			resId = R.drawable.charging_battery_anim_3;
			break;
		case 4:
			resId = R.drawable.charging_battery_anim_4;
			break;
		}

		startBatteryChargeAnim(resId);
	}

	private void switchBatteryImage(int batteryLevel, boolean isCharging, boolean isIdle) {
		int batteryType = 0;	//0 - idle, 1 - charging, 2 - draining
		int resourceId = R.drawable.battery_empty;

		batteryType = (isCharging) ? 1 : 2;
		//idle will override any activity
		batteryType = (isIdle) ? 0 : batteryType;

		switch (batteryType) {
		case 0:
			switch (batteryLevel) {
			case 1:
				resourceId = R.drawable.battery_idle_1;
				break;
			case 2:
				resourceId = R.drawable.battery_idle_2;
				break;
			case 3:
				resourceId = R.drawable.battery_idle_3;
				break;
			case 4:
				resourceId = R.drawable.battery_idle_4;
				break;
			case 5:
				resourceId = R.drawable.battery_idle_5;
				break;
			default:
				break;
			}
			break;
		case 1:
			switch (batteryLevel) {
			case 0:
			case 1:
			case 2:
			case 3:
			case 4:
				startNewBatteryChargingAnim(batteryLevel);
				return;
			case 5:
				resourceId = R.drawable.battery_charge_5;
			default:
				break;
			}

			break;
		case 2:
			switch (batteryLevel) {
			case 1:
				resourceId = R.drawable.battery_drain_1;
				break;
			case 2:
				resourceId = R.drawable.battery_drain_2;
				break;
			case 3:
				resourceId = R.drawable.battery_drain_3;
				break;
			case 4:
				resourceId = R.drawable.battery_drain_4;
				break;
			case 5:
				resourceId = R.drawable.battery_drain_5;
				break;
			default:
				break;
			}
			break;
		}

		mBatteryImageView.setImageResource(resourceId);
	}
}